package com.example.datacollection.config.shiro;

import com.example.datacollection.bean.SysUser;
import com.example.datacollection.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Set;

@Slf4j
public class RealmConfig extends AuthorizingRealm {
    private static org.slf4j.Logger logger = LoggerFactory.getLogger(RealmConfig.class);

    //    //如果项目中用到了事物，@Autowired注解会使事物失效，可以自己用get方法获取值
//    @Autowired
//    private SysRoleService roleService;
//    @Autowired
//    private UserService userService;
    @Autowired
    private UserService userService;

    /**
     * 认证信息.(身份验证) : Authentication 是用来验证用户身份
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken) throws AuthenticationException {
        logger.info("---------------- 执行 Shiro 凭证认证 ----------------------");
        UsernamePasswordToken token = (UsernamePasswordToken) authcToken;
        String name = token.getUsername();
        String password = String.valueOf(token.getPassword());
        SysUser user = new SysUser();
        user.setUserName(name);
        user.setPassWord(password);
        // 从数据库获取对应用户名密码的用户
        SysUser sysUser = userService.getUserInfo(user.getUserName());
//        Subject subject = SecurityUtils.getSubject();
//        subject.getPrincipal();
        log.info("查询结果：" + sysUser);
        if (sysUser != null) {
            // 用户为禁用状态
            if (sysUser.getUserEnable() != 1) {
                throw new DisabledAccountException();
            }
            return new SimpleAuthenticationInfo(sysUser, sysUser.getPassWord(), getName());
        }
        throw new UnknownAccountException();
    }

    /**
     * 授权
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        logger.info("---------------- 执行 Shiro 权限获取 ---------------------");
        System.out.println(principals.getPrimaryPrincipal());
        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
//        if (principal instanceof SysUser) {
//            SysUser userLogin = (SysUser) principal;
//            Set<String> roles = roleService.findRoleNameByUserId(userLogin.getId());
//            authorizationInfo.addRoles(roles);
//
//            Set<String> permissions = userService.findPermissionsByUserId(userLogin.getId());
//            authorizationInfo.addStringPermissions(permissions);
//        }
        //获取当前用户的对象
        //拿到当前这个对象
        Subject subject = SecurityUtils.getSubject();
        SysUser user = (SysUser) subject.getPrincipal();//拿到当前用户
        System.out.println(user.getUserRole());
        //设置当前用户权限
        authorizationInfo.addStringPermission(String.valueOf(user.getUserRole()));
        logger.info("---- 获取到以下权限 ----");
        logger.info(authorizationInfo.getStringPermissions().toString());
        return authorizationInfo;
    }
}
